package io.sunny.platform.backend.common.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author SUNNY
 * @Date 2025/8/31 13:03
 * @PackageName: io.sunny.platform.backend.common.aspect
 * @Description: 统一日志拦截（AOP）
 * @Version 1.0
 */
@Aspect
@Component
public class LoggingAspect {

    private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);

    @Pointcut("execution(* io.sunny.platform.backend.interfaces..*(..))")
    public void controllerMethods() {}

    @Around("controllerMethods()")
    public Object logController(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        logger.info("调用方法: {} 参数: {}", joinPoint.getSignature(), Arrays.toString(joinPoint.getArgs()));
        Object result = joinPoint.proceed();
        logger.info("方法 {} 执行完成，耗时: {}ms", joinPoint.getSignature(), System.currentTimeMillis() - start);
        return result;
    }
}
